(function ($) {
    phoneTime = 60;
    codeTime = 0;

    changeimg = function(d){
        var id = "#" + d;
        $(id).attr("src", Store.context + "/user/code.do?i="+Math.random());
    }


    //    用户名规范
    checkName = function checkName() {
        var nameinput = $("input[name='username']");
        var username = nameinput.val();
        var check = $(".namecheck");

        var rule1 = /^[a-zA-Z][a-zA-Z0-9_]*$/;
        if (username.length == 0) {
            check.html("用户名不能为空");
            return false;
        } else if (username.indexOf(" ") != -1) {
            check.html("用户名不能存在空格");
            return false;
        } else if (!rule1.test(username)) {
            check.html("用户名不符合规范");
            return false;
        } else{
            $.ajax({
                url: Store.context + "/user/check.do",
                type: "post",
                data: {
                    username: username
                },
                success: function (data) {
                    if (data.status == 0) {
                        check.html("该用户不存在");
                        return false;
                    } else {
                        check.html("");
                        return true;
                    }
                }
            })
        }
    }

    checkCode = function () {
        var code = "<%=session.getAttribute('imageCode')%>";
        var code2 = $("input[name='code']").val();
        if(code.toUpperCase() != code2.toUpperCase()){
            $(".codecheck").html("验证码不匹配");
            return false;
        }else{
            $(".codecheck").html("");
            return true;

        }
    }



    //发送短信验证码
    getPhoneCode = function (phone) {
        alert("进来了");
        $.ajax({
            url: Store.context + "/user/sendPhoneCode.do",
            type: 'post',
            data: {
                phone: phone,
            },
            success: function (data) {
                time();
                codetime();
            }
        })
    }



    codetime = function () {
        setInterval(function () {
            codeTime++;
        }, 1000)
    }


    //时间计算器
    time = function () {
        var _this = this;
        var t = $("#phonetime");
        if (phoneTime == 0) {
            codeTime = 0;
            phoneTime = 60;
            t.removeAttr("disabled");
            t.val("获取验证码");
        } else {
            t.attr('disabled', true);
            t.val(phoneTime + "秒后可重新发送");
            phoneTime--;
            setTimeout(function () {
                time();
            }, 1000);
        }
    }

    checkPassword = function () {
        var p1 = $("input[name='password']").val();
        var p2 = $("input[name='passwordconfirm']").val();
        if(p1 == p2 ) {
            return true;
        }else{
            alert("密码不一致");
            return false;
        }
    }

    submit = function () {
        if(checkName()?checkCode():false){
            $(".formsub").submit();
        }
    }

    submit2 = function (uid,phone) {
        var selval = $(".selyz").find("option:selected").val();
        if (selval == '0') {
            $.ajax({
                url: Store.context + "/user/forgetPassword2.do",
                type: "post",
                data: {
                    phone: phone,
                    code: $("#phonecode").val()
                },
                success: function (d) {
                    if (d) {
                        location.href = Store.context+ "/user/gotoforgetPassword3.do?uid=" + uid;
                    } else {
                        if (codeTime == 300) {
                            $(".codecheck").html("您的验证码已过期");
                        } else {
                            $(".codecheck").html("您输入的验证码错误,请重新输入")
                        }

                    }
                }
            })
        }
        if (selval == "1") {
            nodo()
        }
    }

    submit3 = function () {
        if(checkPassword()){
            var p  = $("input[name='password']");
            p.val(hex_md5(p.val()));
            $("#forget3").submit();
        }
    }



    nodo = function () {
        alert("本版本不支持邮箱干活了");
    }
}(jQuery))